<script>
import {User, Search} from '@element-plus/icons-vue'
export default {
  name: 'login',
  data () {
    return {
      user: {
        userName: '',
        userPwd: ''
      },
      rules: {
        userName: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          }
        ],
        userPwd: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    users () {
      return User
    },
    search () {
      return Search
    }
  },
  methods: {
    login () {
      this.$refs.userForm.validate((valid) => {
        if (valid) {
          this.$api.login(this.user).then(res => { 
            this.$store.commit('saveUserInfo', res)
            this.$router.push('/welcome')
          })
        } else {
          return false
        }
      })
    }
    // goHome() {
    //   this.$router.push("/welcome");
    // },
  },
  mounted () {
    /* this.$request({
      method: 'get',
      url: '/login',
      data: {
        name: 'chris'
      }
    }).then( res=> {
      console.log(res)
    })
    // 希望以下面的方式来调用
    this.$request.get('/login', {name: 'jason'}, {mock: true}).then(res => {
      console.log(res)
    }) */
  }
}
</script>

<template>
  <div class="login-wrapper">
    <div class="modal">
      <el-form
        :model="user"
        :rules="rules"
        ref="userForm"
        status-icon
      >
        <div class="title">
          登录页
        </div>
        <el-form-item prop="userName">
          <el-input prefix-icon="el-icon-user" type="text" v-model="user.userName"/>
        </el-form-item>
        <el-form-item prop="userPwd">
          <el-input prefix-icon="el-icon-search" type="password" v-model="user.userPwd" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn-login" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f9fcff;
  width: 100vw;
  height: 100vh;
  .modal {
    width: 500px;
    padding: 50px;
    background-color: #fff;
    border-radius: 4px;
    box-shadow: 0 0 10px 3px #c7c9cb4d;
    .title {
      font-size: 50px;
      line-height: 1.5;
      text-align: center;
      margin-bottom: 30px;
    }
    .btn-login {
      width: 100%;
    }
  }
}
</style>
